package com.liefox.controller;

import com.liefox.pojo.Patient;
import com.liefox.pojo.User;
import com.liefox.service.PatientService;
import com.liefox.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author zjh
 * @Date 2021/4/18 下午 5:30
 **/
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    JavaMailSenderImpl mailSender;
    @Autowired
    private PatientService patientService;

    /**
     * 生成6位随机数验证码
     */
    public static int randomCode() {
        return (int) ((Math.random() * 9 + 1) * 100000);
    }

    /**
     * i:验证码
     */
    static int i = randomCode();

    /*注册*/
    @PostMapping("/sign-up")
    public String signup(User user, Model model) {
        try {
            int i = userService.Signup(user);
            if (i != 0) {
                System.out.println(user + "=》注册成功");
                model.addAttribute("msg", "注册成功！");
                return "/doctor/sign-in";
            }
        } catch (Exception e) {
            System.err.println(user + "=>注册失败");
            model.addAttribute("msg", "该邮箱已注册！");
            return "/doctor/sign-up";
        }
        return null;
    }

    /*登录*/
    @RequestMapping("/sign-in")
    public String signin(User user, Model model, HttpSession session, String Email) {
        User signin = userService.Signin(user);
        User userInfo = userService.getUserInfo(Email);
        System.out.println(userInfo + "用户信息");
        String userName = userService.getUserName(user.getEmail(), user.getPassword());
        if (signin != null) {
            /*用户信息*/
            session.setAttribute("UserInfo", userInfo);
            /*登录拦截*/
            session.setAttribute("loginUser", userName);
            /*获取病人病情信息*/
            List<Patient> patientInfo = patientService.getPatientInfo();
            long count = patientInfo.stream().count();
            model.addAttribute("patientInfos",patientInfo);
            model.addAttribute("count",count);
            /*获取医生信息*/
            List<User> userinfo = userService.getUser();
            model.addAttribute("userInfos",userinfo);
            /**/
            session.setAttribute("Email", Email);
            System.out.println(user + "=》登录成功");
            return "/doctor/index";
        } else {
            System.err.println(user + "=》登录失败");
            model.addAttribute("msg", "邮箱地址或密码错误!");
            return "/doctor/sign-in";
        }

    }
    /*去首页*/
    @RequestMapping("/toindex")
    public String toindex(Model model){
        /*获取病人病情信息*/
        List<Patient> patientInfo = patientService.getPatientInfo();
        model.addAttribute("patientInfos", patientInfo);
        long count = patientInfo.stream().count();
        model.addAttribute("count",count);
        /*获取医生信息*/
        List<User> user = userService.getUser();
        model.addAttribute("userInfos",user);
        return "/doctor/index";
    }

    /*注销*/
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "redirect:/sign-in.html";
    }

    /*忘记密码发邮件*/
    @RequestMapping("/recPwEmail")
    public String recPwEmail(String Email, HttpSession session, Model model) {
        System.out.println(Email + "发送了验证码");
        System.err.println(i);

        session.setAttribute("Email", Email);
        try {
            //邮件设置
            SimpleMailMessage message = new SimpleMailMessage();
            //主题
            message.setSubject("Shiqi-验证码");
            //内容-验证码
            message.setText(String.valueOf(i));
            //收件人
            message.setTo(Email);
            //发件人
            message.setFrom("2606097218@qq.com");
            //发送
            mailSender.send(message);
            model.addAttribute("info", "验证码发送成功！");
            return "/doctor/pages-recoverpw";
        } catch (Exception e) {
            System.err.println("cs");
            model.addAttribute("info", "邮箱地址不正确！");
            return "/doctor/pages-recoverpw";
        }


    }

    /*判断验证码正确,并重置密码*/
    @RequestMapping("/recPw")
    public String recPw(String Email, int token, String Password, Model model) {
        System.out.println(Email + "    重置密码为=》" + Password + "     输入的验证码为     " + i);

        if (token == i) {
            userService.recPw(Email, Password);
            model.addAttribute("info", "修改成功！");
            return "/doctor/sign-in";
        } else {
            model.addAttribute("error", "验证码错误！");
            return "/doctor/pages-recoverpw";
        }

    }

    /*查看个人信息页面*/
    @RequestMapping("/getUserInfo")
    public String getUserInfo() {
        return "/doctor/main/profile-edit";
    }

    /*修改个人信息*/
    @RequestMapping("/updateUserInfo")
    public String updateUserInfo(User user, Model model) {
        int i = userService.updateUserInfo(user);
        if (i != 0) {
            model.addAttribute("msg","下次登录生效！");
            return "/doctor/main/profile-edit";
        }else {
            System.err.println("111111111");
            model.addAttribute("msg","不修改就别乱点！");
            return "/doctor/main/profile-edit";
        }

    }

    /*修改密码*/
    @RequestMapping("/upPw")
    public String upPw(String Email,String Password,Model model){
            userService.upPw(Email, Password);
            model.addAttribute("info","修改成功！");
            return "doctor/sign-in";


    }


}
